//
//  SchedulePickerView.swift
//  EyeVisionForDoctors
//
//  Created by PMC on 2020/3/2.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit

class SchedulePickerView: UIView {
    
    var selectedClosure: ((_ vm: DoctorScheduleViewModel) -> Void)?
    
    private var viewModel: DoctorDetailViewModel? {
        didSet {
            tableView.reloadData()
        }
    }
    
    @IBOutlet private weak var titleLabel: UILabel! {
        didSet {
            titleLabel.textColor = .mainColor
        }
    }
    
    @IBOutlet private weak var activityView: UIActivityIndicatorView!
    
    @IBOutlet private weak var contentView: UIView! {
        didSet {
            contentView.layer.cornerRadius = 16
            contentView.layer.masksToBounds = true
            contentView.transform = CGAffineTransform(scaleX: 0.0001, y: 0.0001)
        }
    }

    @IBOutlet private weak var tableView: UITableView! {
        didSet {
            tableView.delegate = self
            tableView.dataSource = self
            tableView.rowHeight = 50
            tableView.estimatedRowHeight = UITableView.automaticDimension
            tableView.backgroundColor = .white
            tableView.tableFooterView = UIView()
            tableView.register(SchedulePickerCell.nibForCell(),
                               forCellReuseIdentifier: SchedulePickerCell.reuseIdentifier())
        }
    }
    
    @IBAction private func closeAction() {
        dismiss()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        self.alpha = 0.0
    }
    
    func show(in view: UIView? = nil, vm: DoctorDetailViewModel) {
        let tempView = view == nil ? mainWindow : view
        tempView?.addSubview(self)
        
        titleLabel.text = "远程门诊日期"
        
        UIView.animate(withDuration: 0.25, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0, options: .curveLinear, animations: {
            self.alpha = 1.0
            self.contentView.transform = CGAffineTransform(scaleX: 1, y: 1)
        }) { _ in
            self.loadData(vm)
        }
    }
    
    private func dismiss() {
        UIView.animate(withDuration: 0.25, delay: 0, options: .curveLinear, animations: {
            self.contentView.transform = CGAffineTransform(scaleX: 0.0001, y: 0.0001)
        }) { _ in
            self.alpha = 0.0
            self.removeFromSuperview()
        }
    }
}

extension SchedulePickerView {
    private func loadData(_ vm: DoctorDetailViewModel) {
        activityView.startAnimating()
        DoctorDetailViewModel.getDoctorDetail(doctorId: vm.doctorId, success: { model in
            self.activityView.stopAnimating()
            self.viewModel = DoctorDetailViewModel(model: model)
        }) { errorMsg in
            self.activityView.stopAnimating()
            HUDTool.showError(message: errorMsg)
        }
    }
}

extension SchedulePickerView: UITableViewDataSource, UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel?.numberOfRows ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: SchedulePickerCell.reuseIdentifier(),
                                                 for: indexPath) as! SchedulePickerCell
        if let vm = viewModel?.viewModel(for: indexPath) {
            cell.configure(with: vm)
        }
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard let vm = viewModel?.viewModel(for: indexPath) else { return }
        
        switch vm.status {
        case .unwanted:
            dismiss()
            selectedClosure?(vm)
        default:
            HUDTool.show(message: "已约满")
            break
        }
    }
}
